package com.qf.fengmimall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.qf.fengmimall.dto.DetailInfoDto;
import com.qf.fengmimall.product.entity.Product;
import com.qf.fengmimall.product.entity.ProductImg;
import com.qf.fengmimall.product.entity.ProductParams;
import com.qf.fengmimall.product.entity.ProductSku;
import com.qf.fengmimall.product.service.*;
import com.qf.fengmimall.vo.ProductDetailCountVo;
import com.qf.fengmimall.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 刘欣
 * @createTime 2023/2/15 15:35
 * @description 商品接口
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductParamsService productParamsService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private IProductImgService productImgService;

    @Autowired
    private IProductCommentsService productCommentsService;



    /**
     * 商品详情
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail-info/{id}")
    public ResultVo productDetailParams(@PathVariable("id") String id) {
        log.debug("当前接受到的参数为：id={0}", id);

        Product product = productService.getById(id);

        List<ProductImg> productImgs = productImgService.list(new LambdaQueryWrapper<ProductImg>().eq(ProductImg::getItemId, id));

        List<ProductSku> productSkus = productSkuService.list(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, id));

        DetailInfoDto detailInfoDto = new DetailInfoDto(product, productSkus, productImgs);

        return ResultVo.ok(detailInfoDto);
    }


    /**
     * 商品参数
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail-params/{id}")
    public ResultVo productDetailInfo(@PathVariable("id") String id) {
        log.debug("当前接受到的参数为：id={0}", id);

        ProductParams productParams = productParamsService.getById(id);

        return ResultVo.ok(productParams);
    }

    /**
     * 商品评论
     *
     * @param id      商品id
     * @param pageNum 当前页码
     * @param limit   显示信息个数
     * @return
     */
    @RequestMapping("/detail-commonts/{id}")
    public ResultVo ProductDetailCommonts(@PathVariable("id") String id, Integer pageNum, Integer limit) {
        Map<String, Object> dataMap = productCommentsService.pageProductDetailCommonts(id, pageNum, limit);
        return ResultVo.ok(dataMap);
    }

    /**
     * 商品评论
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail-commontscount/{id}")
    public ResultVo ProductDetailCount(@PathVariable("id") String id) {
        ProductDetailCountVo vo = productCommentsService.getProductDetailCount(id);
        return ResultVo.ok(vo);
    }

    @RequestMapping("/addProduct")
    public ResultVo addProduct(@RequestBody ProductParams productParams) throws JsonProcessingException {
        log.debug("接受到的参数productParams={}", productParams);
        productParamsService.addProduct(productParams);
        return ResultVo.ok();
    }
}
